<template>
  <div id="User">
    <el-breadcrumb
      separator="/"
      style="padding-left: 10px; padding-bottom: 10px; font-size: 12px"
    >
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>欢迎</el-breadcrumb-item>
    </el-breadcrumb>
    <el-row :gutter="18">
      <!-- 左边部分，用户信息表 -->
      <el-col :span="12">
        <el-card class="box-card" shadow="hover" tyle="margin-top:10px;">
          <el-row>
            <el-col :span="12">
              <span>用户信息表</span>
            </el-col>
            <el-col :span="12">
              <span style="display: flex; margin-left: 48%"
                ><el-button
                  size="mini"
                  type="warning"
                  icon="el-icon-bangzhu"
                  plain
                  @click="queryPunish"
                  >处分查询</el-button
                ><el-button
                  size="mini"
                  type="primary"
                  icon="el-icon-upload"
                  plain
                  @click="submitTopic"
                  disabled=true
                  >活动申请</el-button
                ></span
              >
            </el-col>
          </el-row>
          <div style="margin-top: 20px">
            <el-table :data="currentUser" border style="width: 100%">
              <el-table-column prop="num" label="学号" align="center">
              </el-table-column>
              <el-table-column prop="username" label="姓名" align="center">
              </el-table-column>
              <el-table-column prop="className" label="班级" align="center">
              </el-table-column>
              <el-table-column label="学生类型" align="center" prop="userType">
              </el-table-column>
              <el-table-column label="总分" align="center" prop="score">
              </el-table-column>
            </el-table>
            <!-- <el-descriptions title="用户信息">
              <el-descriptions-item label="学号">{{
                currentUser.num
              }}</el-descriptions-item>
              <el-descriptions-item label="姓名">{{
                currentUser.username
              }}</el-descriptions-item>
              <el-descriptions-item label="班级">{{
                currentUser.className
              }}</el-descriptions-item>
              <el-descriptions-item label="学生类型">
                <el-tag size="small">{{ currentUser.userType }}</el-tag>
              </el-descriptions-item>
              <el-descriptions-item label="总分">{{
                currentUser.score
              }}</el-descriptions-item>
            </el-descriptions> -->
          </div>
        </el-card>
        <el-tabs type="border-card" value="first" style="margin-top: 10px">
          <el-tab-pane label="院/校级活动" name="first">
            <el-table
              :data="topicList"
              style="width: 100%; margin-top: 10px"
              size="mini"
              height="320px"
            >
              <el-table-column prop="topicName" label="活动名称" align="center">
              </el-table-column>
              <el-table-column
                prop="createTime"
                label="创建时间"
                align="center"
              >
                <template slot-scope="scope">{{
                  scope.row.createTime | dateFormat
                }}</template>
              </el-table-column>
              <el-table-column
                prop="expirationTime"
                label="过期时间"
                align="center"
              >
                <template slot-scope="scope">{{
                  scope.row.expirationTime | dateFormat
                }}</template>
              </el-table-column>
              <el-table-column prop="cover" label="选择类型" align="center">
                <template slot-scope="scope">
                  <el-tag v-if="scope.row.compulsory" size="mini" type="danger"
                    >必选</el-tag
                  >
                  <el-tag v-else size="mini" type="success">可选</el-tag>
                </template>
              </el-table-column>
              <el-table-column
                prop="supportUploadScore"
                label="分数"
                align="center"
              >
                <template slot-scope="scope">
                  <el-tag size="mini">{{
                    scope.row.supportUploadScore
                  }}</el-tag>
                </template></el-table-column
              >
              <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                  <el-button
                    v-if="scope.row.selected == 1"
                    type="text"
                    size="mini"
                    @click="particapateCurrentTopic(scope.row.topicId)"
                    >{{
                      scope.row.attendanceCount == 0
                        ? scope.row.joined
                        : scope.row.attendanceCount
                    }}/{{
                      scope.row.limited == null ? "无上限" : scope.row.limited
                    }}/参加</el-button
                  >
                  <el-button
                    v-if="scope.row.selected == 2"
                    type="text"
                    size="mini"
                    disabled
                    style="color: #13ce66"
                    >{{ scope.row.attendanceCount }}人/{{
                      scope.row.limited == null ? "无上限" : scope.row.limited
                    }}</el-button
                  >
                  <el-button
                    v-if="scope.row.selected == 3"
                    type="text"
                    size="mini"
                    disabled
                    >{{ scope.row.attendanceCount }}/{{
                      scope.row.limited == null ? "无上限" : scope.row.limited
                    }}/已过期</el-button
                  >
                </template>
              </el-table-column>
            </el-table></el-tab-pane
          >
          <el-tab-pane label="班级活动" name="second">
            <el-table
              :data="classTopicList"
              style="width: 100%; margin-top: 10px"
              size="mini"
              height="320px"
            >
              <el-table-column prop="topicName" label="活动名称" align="center">
              </el-table-column>
              <el-table-column
                prop="createTime"
                label="创建时间"
                align="center"
              >
                <template slot-scope="scope">{{
                  scope.row.createTime | dateFormat
                }}</template>
              </el-table-column>
              <el-table-column
                prop="expirationTime"
                label="过期时间"
                align="center"
              >
                <template slot-scope="scope">{{
                  scope.row.expirationTime | dateFormat
                }}</template>
              </el-table-column>
              <el-table-column prop="cover" label="选择类型" align="center">
                <template slot-scope="scope">
                  <el-tag v-if="scope.row.compulsory" size="mini" type="danger"
                    >必选</el-tag
                  >
                  <el-tag v-else size="mini" type="success">可选</el-tag>
                </template>
              </el-table-column>
              <el-table-column
                prop="supportUploadScore"
                label="分数"
                align="center"
              >
                <template slot-scope="scope">
                  <el-tag size="mini">{{
                    scope.row.supportUploadScore
                  }}</el-tag>
                </template></el-table-column
              >
              <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                  <el-button
                    v-if="scope.row.selected == 1"
                    type="text"
                    size="mini"
                    @click="particapateCurrentTopic(scope.row.topicId)"
                    >{{
                      scope.row.attendanceCount == 0
                        ? scope.row.joined
                        : scope.row.attendanceCount
                    }}/{{
                      scope.row.limited == null ? "无上限" : scope.row.limited
                    }}/参加</el-button
                  >
                  <el-button
                    v-if="scope.row.selected == 2"
                    type="text"
                    size="mini"
                    disabled
                    style="color: #13ce66"
                    >{{ scope.row.attendanceCount }}人/{{
                      scope.row.limited == null ? "无上限" : scope.row.limited
                    }}</el-button
                  >
                  <el-button
                    v-if="scope.row.selected == 3"
                    type="text"
                    size="mini"
                    disabled
                    >{{ scope.row.attendanceCount }}/{{
                      scope.row.limited == null ? "无上限" : scope.row.limited
                    }}/已过期</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </el-col>
      <!-- 右边部分 -->
      <el-col :span="12">
        <el-card class="box-card" shadow="hover">
          <span>吐槽栏</span>
          <el-table
            :data="issueList"
            style="width: 100%; margin-top: 10px"
            size="small"
            height="240px"
          >
            <el-table-column prop="issueContent" label="吐槽" width="350">
            </el-table-column>
            <el-table-column
              prop="username"
              label="姓名"
              width="180"
              align="center"
            >
            </el-table-column>
            <el-table-column prop="releaseTime" label="时间" align="center">
              <template slot-scope="scope">{{
                scope.row.releaseTime | dateFormat
              }}</template>
            </el-table-column>
          </el-table>
        </el-card>
        <el-card class="box-card" shadow="hover" style="margin-top: 10px">
          <!-- 库存饼图 -->
          <div id="pieChart" style="width: 590px; height: 232px"></div>
        </el-card>
      </el-col>
    </el-row>
    <el-dialog title="上传活动" :visible.sync="show" width="400px">
      <el-form
        :model="currentTopic"
        align="center"
        ref="topicRuleRef"
        :rules="topicRule"
      >
        <el-form-item
          label="活动类型"
          label-width="80px"
          prop="topicType"
          :rules="[
            {
              required: true,
              message: '活动类型不能为空',
              trigger: 'blur',
            },
          ]"
        >
          <el-select
            v-model="currentTopic.topicType"
            placeholder="请选择活动类型"
          >
            <el-option
              v-for="item in topicTypes"
              :key="item.topicTypeId"
              :label="item.topicTypeName"
              :value="item.topicTypeId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="活动名称"
          label-width="80px"
          :rules="[
            {
              required: true,
              message: '活动名称不能为空',
              trigger: 'blur',
            },
          ]"
          prop="topicName"
        >
          <el-input
            v-model="currentTopic.topicName"
            autocomplete="off"
            style="width: 220px"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="上报分数"
          label-width="80px"
          :rules="[
            {
              required: true,
              message: '分数不能为空',
              trigger: 'blur',
            },
          ]"
          prop="uploadScore"
        >
          <el-input-number
            v-model="currentTopic.uploadScore"
            style="width: 220px"
            :step="0.1"
            :min="0.1"
            :max="10"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="loading" @click="saveTopic"
          >确 定</el-button
        >
        <el-button @click="show = false">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="处分查询" :visible.sync="punishShow" width="520px">
      <el-table :data="currentStuPunishList" style="width: 100%" align="center">
        <el-table-column prop="punishName" label="处分名称" align="center">
        </el-table-column>
        <el-table-column prop="punishTime" label="处分时间" align="center">
          <template slot-scope="scope">{{
            scope.row.punishTime | dateFormat
          }}</template>
        </el-table-column>
        <el-table-column prop="score" label="分数" align="center">
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>
<script>
import echarts from "echarts";
import { mapState } from "vuex";
import {
  queryCurrentUserTopicList,
  queryTopicTypes,
  uploadTopicByStu,
  joinCurrentTopic,
  queryLatestIssue,
  queryTypeScore,
  queryCurrentStuPunish,
} from "api/api";
export default {
  computed: {
    ...mapState(["userInfo"]),
  },
  data() {
    return {
      currentUser: [],
      queryMap: {
        uploadType: true,
        cover: true,
      },
      topicList: [],
      currentTopic: {},
      show: false,
      topicTypes: [],
      loading: false,
      issueList: [],
      pieChart: [], //存放活动类型名称
      selected: {}, //存放选择的数据
      seriesData: [{ name: "", value: "" }], //饼图数据
      punishShow: false,
      currentStuPunishList: [],
      classTopicList: [],
      currentTime: "", //当前时间
      topicRule: {}, //认证规则
    };
  },
  methods: {
    //改变页码
    handleSizeChange(newSize) {
      this.queryMap.size = newSize;
      this.getCurrentUserTopicList();
    },
    //翻页
    handleCurrentChange(current) {
      this.queryMap.page = current;
      this.getCurrentUserTopicList();
    },
    //获取当前用户
    getCurrentUser() {
      this.currentUser.push(JSON.parse(window.sessionStorage.getItem("token")));
      if (this.currentUser[0].userType == 1) {
        this.currentUser[0].userType = "本科";
      } else if (this.currentUser[0].userType == 2) {
        this.currentUser[0].userType = "专升本";
      } else if (this.currentUser[0].userType == 3) {
        this.currentUser[0].userType = "专科";
      }
    },
    //获取当前用户的活动列表
    getCurrentUserTopicList() {
      queryCurrentUserTopicList(this.userInfo.sessionId, this.queryMap).then(
        (res) => {
          if (!res.flag) {
            this.$message.error(res.message);
          } else {
            this.topicList = res.data;
          }
        }
      );
      this.queryMap.queryType = 1;
      queryCurrentUserTopicList(this.userInfo.sessionId, this.queryMap).then(
        (res) => {
          if (!res.flag) {
            this.$message.error(res.message);
          } else {
            this.classTopicList = res.data;
          }
        }
      );
    },
    //获取活动类型列表
    getTopicTypes() {
      queryTopicTypes(this.userInfo.sessionId).then((res) => {
        if (!res.flag) {
          this.$message.error(res.message);
        } else {
          this.topicTypes = res.data;
        }
      });
    },
    //活动申请
    submitTopic() {
      this.loading = true;
      this.show = true;
      this.loading = false;
    },
    //活动上传
    saveTopic() {
      this.loading = true;
      this.$refs.topicRuleRef.validate((valid) => {
        if (!valid) {
          this.loading = false;
          return;
        } else {
          uploadTopicByStu(
            this.userInfo.sessionId,
            this.currentTopic,
            this.currentTopic.uploadScore
          )
            .then((res) => {
              if (!res.flag) {
                this.$message.error(res.message);
                this.loading = false;
              } else {
                this.$message({
                  type: "success",
                  message: "新增成功！",
                });
                this.loading = false;
                this.show = false;
              }
            })
            .catch((err) => {
              this.loading = false;
            });
        }
      });
    },
    //参加当前活动
    async particapateCurrentTopic(topicId) {
      await joinCurrentTopic(this.userInfo.sessionId, topicId).then((res) => {
        if (!res.flag) {
          this.$message.error(res.message);
        } else {
          this.$message({
            type: "success",
            message: "参加成功，审核中，请耐心等待！",
          });
          this.getCurrentUserTopicList();
        }
      });
    },
    //查询最新吐槽
    getLatestIssue() {
      queryLatestIssue(this.userInfo.sessionId).then((res) => {
        if (!res.flag) {
          this.$message.error(res.message);
        } else {
          this.issueList = res.data;
        }
      });
    },
    //获取当前用户的各个活动得分
    getCurrentUserTopicTypeScore() {
      queryTypeScore(this.userInfo.sessionId).then((res) => {
        if (!res.flag) {
          this.$message.error({
            type: "warning",
            message: res.message,
          });
        } else {
          this.pieChart = [];
          this.selected = {};
          this.seriesData = [];
          var $this = this;
          res.data.forEach(function (e) {
            $this.pieChart.push(e.topicTypeName);
            $this.seriesData.push({
              name: e.topicTypeName,
              value: e.score,
            });
          });
          this.drawPieChart();
        }
      });
    },
    //绘制饼图
    drawPieChart() {
      var myChart = echarts.init(document.getElementById("pieChart"));
      var option = {
        title: {
          text: "各活动类型得分情况",
          left: "left",
        },
        toolbox: {
          show: true,
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)",
        },
        legend: {
          type: "scroll",
          orient: "vertical",
          right: 10,
          top: 50,
          bottom: 20,
          data: this.pieChart,
          selected: this.selected,
        },
        series: [
          {
            name: "活动类型",
            type: "pie",
            radius: "55%",
            center: ["40%", "50%"],
            data: this.seriesData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      };
      myChart.setOption(option);
    },
    //查询当前学生的处分信息
    getCurrentStuPunish() {
      queryCurrentStuPunish(this.userInfo.sessionId).then((res) => {
        if (!res.flag) {
          this.$message({
            type: "success",
            message: res.message,
          });
          this.punishShow = false;
        } else {
          this.punishShow = true;
          this.currentStuPunishList = res.data;
        }
      });
    },
    //查询处分点击事件
    queryPunish() {
      this.getCurrentStuPunish();
    },
  },
  mounted() {
    this.currentTime = new Date();
    this.getCurrentUser();
    this.getCurrentUserTopicList();
    this.getTopicTypes();
    this.getLatestIssue();
    this.getCurrentUserTopicTypeScore();
  },
};
</script>
<style scoped>
.el-table .warning-row {
  background: oldlace;
}
</style>